//
//  WTAFNetWorking.m
//  艾听歌
//
//  Created by dllo on 16/6/5.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "WTAFNetWorking.h"

@implementation WTAFNetWorking

+(void)WT_GetWithURL:(NSString *)urlStr Parameter:(NSDictionary *)dic success:(successBlock)sucblock error:(failureBlock)errorBlock
{
    
    if ([self checkNetworkingStatues]) {
        
        NSString *encodString = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain", @"application/x-javascript", @"application/javascript", nil]];
        
        [manager GET:encodString parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            
            //如果请求成功 , 回调请求到的数据 , 同时 在这里 做本地缓存
            NSString *path = [NSString stringWithFormat:@"%ld.plist", [urlStr hash]];
            // 存储的沙盒路径
            NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            //            NSLog(@"%@",path_doc);
            // 归档
            [NSKeyedArchiver archiveRootObject:responseObject toFile:[path_doc stringByAppendingPathComponent:path]];
            
            
            // 将最新的数据存入本地数据库
            
            sucblock(responseObject);
            
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            errorBlock(error);
            
        }];
    }else
    {
        
        // 在这里读取本地缓存
        NSString *path = [NSString stringWithFormat:@"%ld.plist", [urlStr hash]];
        NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        
        id result = [NSKeyedUnarchiver unarchiveObjectWithFile:[path_doc stringByAppendingPathComponent:path]];
        
        if (result) {
            sucblock(result);
        }
        
    }
}

+(void)WT_POSTWithURL:(NSString *)urlStr Parameter:(NSDictionary *)dic success:(successBlock)sucblock error:(failureBlock)errorBlock
{
    
    if ([self checkNetworkingStatues]) {
        
        NSString *encodeString = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain", @"application/x-javascript", @"application/javascript", nil]];
        
        [manager POST:encodeString parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            //如果请求成功 , 回调请求到的数据 , 同时 在这里 做本地缓存
            NSString *path = [NSString stringWithFormat:@"%ld.plist", [urlStr hash]];
            // 存储的沙盒路径
            NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            NSLog(@"%@",path_doc);
            // 归档
            [NSKeyedArchiver archiveRootObject:responseObject toFile:[path_doc stringByAppendingPathComponent:path]];
            
            
            // 将最新的数据存入本地数据库
            
            
            
            sucblock(responseObject);
            
            
            
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            errorBlock(error);
            NSLog(@"失败");
            
        }];
        
    }else
    {
        // 在这里读取本地缓存
        NSString *path = [NSString stringWithFormat:@"%ld.plist", [urlStr hash]];
        NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        
        id result = [NSKeyedUnarchiver unarchiveObjectWithFile:[path_doc stringByAppendingPathComponent:path]];
        
        if (result) {
            sucblock(result);
        }
        
        
        // 从本地取
        //  NSLog(@"缓存");
    }
    
}


+(BOOL)checkNetworkingStatues
{
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    
    if (reachability.currentReachabilityStatus == NotReachable) {
        
        return NO;
        
    }else
    {
        return YES;
    }
}




@end
